package controler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Academy;
import model.Subject;
import model.beans.TopicBean;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.MappingDispatchAction;

import business.Unidades;
import business.misc.Util;

public class TopicAction extends MappingDispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
    	if(action_retVal!=null)return action_retVal;
    	
    	int int_unitId = Integer.parseInt(request.getParameter("unit_id"));
    	    	
    	String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));    	
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
    	request.setAttribute("unidad", Unidades.instance(int_unitId,str_academyCode ,str_subjectCode ));

    	request.setAttribute("lst_temas", business.Temas.list(int_unitId, str_academyCode, str_subjectCode));
    	
		return mapping.findForward(Util.DEFAULT); 
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		DynaActionForm forma = (DynaActionForm)form;
		forma.initialize(mapping);
		forma.reset(mapping, request);
		
		int int_unitId = Integer.parseInt(request.getParameter("unit_id"));
		String str_academyCode = request.getParameter("academy_id");
		String str_subjectCode = request.getParameter("subject_id");
		request.setAttribute("academia", Academy.instance(str_academyCode));
		request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
		request.setAttribute("unidad", Unidades.instance(int_unitId,str_academyCode , str_subjectCode)); 
		
		return mapping.findForward(Util.DEFAULT); 
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		int int_topicKey = Integer.parseInt(request.getParameter("topic_id"));
		int int_unitId = Integer.parseInt(request.getParameter("unit_id"));
		String str_academyCode = request.getParameter("academy_id");
		String str_subjectCode = request.getParameter("subject_id");
		request.setAttribute("academia", Academy.instance(str_academyCode));
		request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));
		request.setAttribute("unidad", Unidades.instance(int_unitId,str_academyCode , str_subjectCode)); 
		
		DynaActionForm forma = (DynaActionForm)form;
		TopicBean bn_topic = business.Temas.instance(int_topicKey, str_academyCode, str_subjectCode );
		bn_topic.setAcademy(str_academyCode);
		bn_topic.setSubject(str_subjectCode);
		forma.set("int_topicKey", new Integer(bn_topic.getInt_topicId()));
		forma.set("int_topicNumber", new Integer(bn_topic.getInt_topicNumber()));
		forma.set("str_descripcion", bn_topic.getStr_descripcion());
		forma.set("int_unitNumber", new Integer(bn_topic.getInt_unitNumber()));
				
		return mapping.findForward(Util.DEFAULT); 
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		DynaActionForm forma = (DynaActionForm)form;
		String str_forward;
		
		int int_unitId = Integer.parseInt(request.getParameter("unit_id"));
		String str_academyCode = request.getParameter("academy_id");
    	String str_subjectCode = request.getParameter("subject_id");
    	request.setAttribute("academia", Academy.instance(str_academyCode));    	
    	request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));		
		
		if( ((Integer)forma.get("int_topicKey")).intValue() == 0 ){
			if( business.Temas.add(forma.getMap(), int_unitId) > 0 ){
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessAdd"));
				str_forward = Util.SUCCESS;
			}else{
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
				str_forward = Util.FAIL+Util.ADD;
			}
		}else{
			if( business.Temas.edit(forma.getMap()) > 0 ){
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessEdit"));
				str_forward = Util.SUCCESS;
			}else{
				request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.FailAdd"));
				str_forward = Util.FAIL+Util.ADD;
			}
		}
		
		if(str_forward.equals(Util.SUCCESS)){
			request.setAttribute("lst_temas", business.Temas.list(int_unitId, str_academyCode, str_subjectCode));
			request.setAttribute("unidad", Unidades.instance(int_unitId,str_academyCode , str_subjectCode)); 
		}
		return mapping.findForward(str_forward); 
	
}
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ActionForward action_retVal = Validate.validateUser(mapping,request, getResources(request));
		if(action_retVal!=null)return action_retVal;
		
		int int_topicKey =Integer.parseInt(request.getParameter("topic_id"));
		int int_unitId = Integer.parseInt(request.getParameter("unit_id"));
		String str_academyCode = request.getParameter("academy_id");
		String str_subjectCode = request.getParameter("subject_id");		
		
		request.setAttribute("academia", Academy.instance(str_academyCode));
		request.setAttribute("materia", Subject.instance(str_subjectCode, str_academyCode));		
		request.setAttribute("unidad", Unidades.instance(int_unitId,str_academyCode ,str_subjectCode ));
		
		if( business.Temas.delete(int_topicKey, int_unitId, str_academyCode, str_subjectCode) > 0 ){
			request.setAttribute("req_msg", getResources(request).getMessage("DraftManage.SuccessDelete"));
		}else{
			request.setAttribute("req_msg", getResources(request).getMessage("Tema.errors.FailDelete"));
		}		
		request.setAttribute("lst_temas", business.Temas.list(int_unitId, str_academyCode, str_subjectCode));
		
		return mapping.findForward(Util.DEFAULT);
	}

}
